﻿// ---------------------------------------------------------------------------------------------
// <copyright file="StronglyConnectedComponent.cs" company="Daniel Bradley">
//   Copyright (c) 2015 All Rights Reserved
// </copyright>
// <summary>
//   cf. https://github.com/danielrbradley/CycleDetection
// </summary>
// ---------------------------------------------------------------------------------------------

using System.Collections;
using System.Collections.Generic;

namespace Hiyoko.DependenciesSorter.CycleDetection
{
    public class StronglyConnectedComponent<T> : IEnumerable<Vertex<T>>
    {
        #region Private members

        private readonly LinkedList<Vertex<T>> _list;

        #endregion Private members

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the StronglyConnectedComponent class.
        /// </summary>
        public StronglyConnectedComponent()
        {
            _list = new LinkedList<Vertex<T>>();
        }

        /// <summary>
        /// Initializes a new instance of the StronglyConnectedComponent class.
        /// </summary>
        /// <param name="collection">The collection of vertex.</param>
        public StronglyConnectedComponent(IEnumerable<Vertex<T>> collection)
        {
            _list = new LinkedList<Vertex<T>>(collection);
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// Gets the number of vertex present in the vertex collection.
        /// </summary>
        public int Count
        {
            get
            {
                return _list.Count;
            }
        }

        /// <summary>
        /// Is cycle
        /// </summary>
        public bool IsCycle { get { return _list.Count > 1; } }

        #endregion

        #region Public methods

        /// <summary>
        /// Adds a vertex to the vertex collection.
        /// </summary>
        /// <param name="vertex">The vertex to add to the collection.</param>
        public void Add(Vertex<T> vertex)
        {
            _list.AddLast(vertex);
        }

        /// <summary>
        /// Gets the enumerator corresponding to the vertex collection.
        /// </summary>
        /// <returns></returns>
        public IEnumerator<Vertex<T>> GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        public List<Vertex<T>> GetList()
        {
            return new List<Vertex<T>>(_list);
        }

        #endregion Public methods
    }
}
